import os
import json
from git import Repo

def git_push(c):
    path = r'D:\StudyProject\BlogProject\GiteeShare\share'
    repo = Repo(path)
    git = repo.git
    git.add('.')
    git.commit('-m', c)
    git.push("origin", "master")


def get_file_list(path, path_index):
    gitee_url1 = 'https://gitee.com/giteehyj/share/raw/master/hiker/source/我的图集/'
    gitee_url2 = 'https://giteehyj.gitee.io/share/hiker/source/我的图集/'
    
    category = os.listdir(path)
    category = [c for c in category if os.path.isdir(os.path.join(path,c))]
        
    index_j = []
    for c in category:
        index_dic = {}
        index_dic['title'] = c
        index_dic['url'] = os.path.join(path_index,c+'.json').replace('.\\',gitee_url1).replace('\\', '/')
        index_j.append(index_dic)
        
        p = os.path.join(path, c)
        titles = os.listdir(p)
        with open(os.path.join(path_index, c+'.json'), 'w') as f:
            j = []
            for t in titles:
                dic = {}
                dic['title'] = t
                dic['urls'] = [os.path.join(p,t,n).replace('.\\',gitee_url2).replace('\\', '/') for n in os.listdir(os.path.join(p,t))]
                j.append(dic)
            f.write(json.dumps(j))
            
    with open(os.path.join(path_index, 'index.json'), 'w') as f:
        f.write(json.dumps(index_j))
    
get_file_list(r'.\img', r'.\index')

flag = input("更新完成，是否提交到远程仓库（y/n）")
if flag!='n':
    git_push("update 我的图集")
    input("提交完成！")
else:
    input("运行完毕，未提交远程仓库")
